﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageEmailCategories : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_EmailCATEGORY = "__EmailCategory::DC";
    private const string PROP_LAYOUT = "__EmailCategory::UIL";
    private const string PROP_SELECTED_EmailCATEGORY_ID = "__EmailCategory::SDCID";

    #endregion

    #region Public Properties

    public EmailCategoryCollection DataSourceEmailCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_EmailCATEGORY] is EmailCategoryCollection ? (EmailCategoryCollection)SessionMgr.Session[PROP_DATASRC_EmailCATEGORY] : (EmailCategoryCollection)(SessionMgr.Session[PROP_DATASRC_EmailCATEGORY] = new EmailCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_EmailCATEGORY] = value; }
    }

    public int SelectedEmailCategoryID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_EmailCATEGORY_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_EmailCATEGORY_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_EmailCATEGORY_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceEmailCategory = EmailCategory.GetAllEmailCategories();
            EmailCategoryList1.DataBind(DataSourceEmailCategory);
        }
    }

    #endregion

    #region Control: EmailCategoryList

    protected void ucEmailCategoryList_EditRequested(object sender, Controls_EmailCategoryList.EditRequestedEventArgs evt)
    {
        SelectedEmailCategoryID = evt.EC.EmailCategoryID;
        EmailCategoryViewEdit1.DataBind(evt.EC);
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;
    }


    protected void ucEmailCategoryList_AddNewRequested(object sender, EventArgs evt)
    {
        EmailCategoryViewEdit1.CancelEdit();
        EmailCategory newEmailCategory = EmailCategory.CreateNew(DataSourceEmailCategory);
        SelectedEmailCategoryID = newEmailCategory.EmailCategoryID;
        EmailCategoryViewEdit1.DataBind(DataSourceEmailCategory.FindByEmailCategoryID(SelectedEmailCategoryID));
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;
    }


    protected void ucEmailCategoryList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceEmailCategory = EmailCategory.GetAllEmailCategories();
        EmailCategoryList1.DataSource = DataSourceEmailCategory;
    }

    #endregion

    #region Control: EmailCategoryViewEdit

    protected void ucEmailCategoryViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        EmailCategoryViewEdit1.DataSourceEmailCategory = DataSourceEmailCategory.FindByEmailCategoryID(SelectedEmailCategoryID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        EmailCategoryViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourceEmailCategory.FindByEmailCategoryID(SelectedEmailCategoryID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceEmailCategory.Remove(DataSourceEmailCategory.FindByEmailCategoryID(SelectedEmailCategoryID));
                SelectedEmailCategoryID = (DataSourceEmailCategory[0] != null ? DataSourceEmailCategory[0].EmailCategoryID : 0);
            }
            catch
            {
            }
        }
        EmailCategoryList1.DataBind(DataSourceEmailCategory);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (EmailCategoryViewEdit1.EndEdit())
        {
            EmailCategory obj = DataSourceEmailCategory.FindByEmailCategoryID(SelectedEmailCategoryID);
            EmailCategory.Update(obj);

            // TODO: Set Feedback text...

            EmailCategoryList1.DataBind(DataSourceEmailCategory);
            SetLayout(LayoutType.View);
            divexistname.Visible = false;
        }
        else
        {
            divexistname.Visible = true;
        }

    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlEmailCategoryList.Visible = false;
                pnlEmailCategoryViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlEmailCategoryList.Visible = true;
                pnlEmailCategoryViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion

}
